<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content ">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-content">
						<form class="form-horizontal m-t" id="signupForm">
							<input type="hidden" name="roleIds" id="roleIds">
							<div class="form-group">
								<label class="col-sm-3 control-label">姓名：</label>
								<div class="col-sm-8">
									<input id="name" name="name" class="form-control" type="text">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">用户名：</label>
								<div class="col-sm-8">
									<input id="username" name="username" class="form-control"
										type="text">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">密码：</label>
								<div class="col-sm-8">
									<input id="password" name="password" class="form-control"
										type="password">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">E-mail：</label>
								<div class="col-sm-8">
									<input id="email" name="email" class="form-control"
										type="email">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">状态:</label>
								<div class="col-sm-8">
									<label class="radio-inline"> <input type="radio"
										 value="1" name="status" checked/> 正常
									</label> <label class="radio-inline"> <input type="radio"
										 value="0" name="status"/> 禁用
									</label>
								</div>
							</div>		
							<div class="form-group">
								<label class="col-sm-3 control-label">角色</label>
								<div class="col-sm-8">
									<label class="checkbox-inline" th:each="role:${roleList}">
										<input name="role" type="checkbox" th:value="${role.roleId}" th:text="${role.roleName}">
									</label>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-8 col-sm-offset-3">
									<button type="submit" class="btn btn-primary">提交</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>

	</div>
	<div th:include="include::footer"></div>
</body>

<script>
	$.validator.setDefaults({
		submitHandler:function(){
		    save();
		}
	})
	$(function(){
		$('#signupForm').validate({
            rules:{
                name:{
                    required:true
				},
				username:{
                    required:true,
					minlength:4,
                    remote:{
                        url:'/sys/user/hasUser',
                        data:{
                            username:function(){
                                return $('#username').val();
                            }
                        },
                        dataType:'json',
                        type:'post'
                    }
				},
				email:{
                   email:true
				}
			},
			messages:{
                name:{
                    required:"用户姓名不能为空",
				},
				username:{
                    required:"用户姓名不能为空",
                    minlength:"用户名不能少于4位",
                    remote:"用户已存在"
				},
                email:{
                    email:"请输入正确的邮箱"
                }
			}
		})
	})


	function save(){
	   //  查询出 所选择的角色ID
		getSelectIds();
		$.ajax({
			url:'/sys/user/save',
			data:$('#signupForm').serialize(),
			dataType:'json',
			type:'post',
			success:function(data){
				if(data.code==200){
					parent.reLoad();
					var frameIndex = parent.layer.getFrameIndex(window.name);
					parent.layer.close(frameIndex);
				}else {
					parent.layer.alert("添加失败,"+data.msg,{icon:2});
				}
			}
		});
	}

	function getSelectIds(){
	    var roleIds = [];
	    $('input[name=role]:checked').each(function(i){
            roleIds.push($(this).val());
		})
		$('#roleIds').val(roleIds);
	}
</script>
</html>
